package cn.parllay.dingdangproject.client;

import java.util.concurrent.TimeUnit;

import cn.parllay.dingdangproject.client.handler.NettyClientHandler;
import cn.parllay.dingdangproject.entity.AskMsg;
import cn.parllay.dingdangproject.entity.AskParams;
import cn.parllay.dingdangproject.entity.BaseMsg;
import cn.parllay.dingdangproject.entity.Constants;
import cn.parllay.dingdangproject.entity.LoginMsg;
import cn.parllay.dingdangproject.entity.MsgType;
import cn.parllay.dingdangproject.entity.PingMsg;
import cn.parllay.dingdangproject.util.Utils_js;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;

public class NettyClientBootstrap {

	    private int port=9999;
	    private String host="127.0.0.1";
	    private SocketChannel socketChannel;
	    @SuppressWarnings("unused")
		private static final EventExecutorGroup group = new DefaultEventExecutorGroup(20);
	    public NettyClientBootstrap() throws InterruptedException {
	        start();
	    }
	    public NettyClientBootstrap(int port, String host) throws InterruptedException {
	        this.port = port;
	        this.host = host;
	        start();
	    }
	    private void start() throws InterruptedException {
	        EventLoopGroup eventLoopGroup=new NioEventLoopGroup();
	        Bootstrap bootstrap=new Bootstrap();
	        bootstrap.channel(NioSocketChannel.class);
	        bootstrap.option(ChannelOption.SO_KEEPALIVE,true);
	        bootstrap.group(eventLoopGroup);
	        bootstrap.remoteAddress(host,port);
	        bootstrap.handler(new ChannelInitializer<SocketChannel>() {
	            @Override
	            protected void initChannel(SocketChannel socketChannel) throws Exception {
	            	//心跳包     第一个参数 30 表示读操作空闲时间      第二个参数 60 表示写操作空闲时间    第三个参数 100 表示读写操作空闲时间      第四个参数 单位
	            	socketChannel.pipeline().addLast(new IdleStateHandler(30, 60, 120, TimeUnit.SECONDS));
	                socketChannel.pipeline().addLast(new ObjectEncoder());
	                socketChannel.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
	                socketChannel.pipeline().addLast(new NettyClientHandler());
	            }
	        });
	        ChannelFuture future =bootstrap.connect(host,port).sync();
	        if (future.isSuccess()) {
	            socketChannel = (SocketChannel)future.channel();
	            System.out.println("connect server  成功---------");
	        }
	    }
	    
	    //断线重连
	    public void reConnectServer(){
	        try {
	            Thread.sleep(5000);
	            System.err.println("客户端进行断线重连");  
	            start();
	        } catch (Exception e) {
	            e.printStackTrace();  
	        }
	    }
	    
	    @SuppressWarnings("static-access")
		public static void main(String[]args) throws InterruptedException {
	        //NettyClientBootstrap bootstrap=new NettyClientBootstrap(9999,"47.104.82.9");
	        NettyClientBootstrap bootstrap=new NettyClientBootstrap(9999,"api.retail.vip");
	    	//NettyClientBootstrap bootstrap=new NettyClientBootstrap(9999,"127.0.0.1");
	    	
	        String storeNO="B11S001120180119180637";
	        LoginMsg loginMsg=new LoginMsg();
	        loginMsg.setUserName(storeNO);
	        loginMsg.setPassword(storeNO);
	        bootstrap.socketChannel.writeAndFlush(loginMsg);
	        
	        TimeUnit.SECONDS.sleep(10);
            PingMsg pingMsg=new PingMsg(); 
            pingMsg.setClientId(storeNO);
            bootstrap.socketChannel.writeAndFlush(pingMsg);
            
            TimeUnit.SECONDS.sleep(10);
            /*BaseMsg loginoutMsg=new BaseMsg(); 
            loginoutMsg.setClientId("B02S00021519911729729");
            loginoutMsg.setType(MsgType.LOGOUT);
            bootstrap.socketChannel.writeAndFlush(loginoutMsg);*/
            
            
	        /*while (true){
	            TimeUnit.SECONDS.sleep(30);
                PingMsg pingMsg=new PingMsg(); 
                pingMsg.setClientId(storeNO);
	            bootstrap.socketChannel.writeAndFlush(pingMsg);
	            
	            TimeUnit.SECONDS.sleep(20);
	            BaseMsg loginoutMsg=new BaseMsg(); 
	            loginoutMsg.setClientId(storeNO);
	            loginoutMsg.setType(MsgType.LOGOUT);
	            bootstrap.socketChannel.writeAndFlush(loginoutMsg);
	            AskMsg askMsg=new AskMsg();
	            askMsg.setClientId(storeNO);
	            AskParams askParams=new AskParams();
	            askParams.setAuth("authToken");
	            askMsg.setParams(askParams);
	            bootstrap.socketChannel.writeAndFlush(askMsg);
	            
	            
	            AskMsg askMsg=new AskMsg();
	            askMsg.setClientId("B02S000220180112115505");
	            AskParams askParams=new AskParams();
	            askParams.setAuth("authToken");
	            askMsg.setParams(askParams);
	            bootstrap.socketChannel.writeAndFlush(askMsg);
	        }*/
	        
	    }
}
